package com.sixmac.service.impl;

import com.sixmac.core.Constant;
import com.sixmac.dao.OperaDao;
import com.sixmac.dao.SysUserDao;
import com.sixmac.entity.Opera;
import com.sixmac.entity.SysUser;
import com.sixmac.service.OperaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/4/11 0011 下午 1:17.
 */
@Service
public class OperaServiceImpl implements OperaService {

    @Autowired
    private OperaDao operaDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public List<Opera> findAll() {
        return operaDao.findAll();
    }

    @Override
    public Page<Opera> find(int pageNum, int pageSize) {
        return operaDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Opera> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Opera getById(int id) {
        return operaDao.findOne(id);
    }

    @Override
    public Opera deleteById(int id) {
        Opera opera = getById(id);
        operaDao.delete(opera);
        return opera;
    }

    @Override
    public Opera create(Opera opera) {
        return operaDao.save(opera);
    }

    @Override
    public Opera update(Opera opera) {
        return operaDao.save(opera);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<Opera> page(final String name, final String description, final Date startTime, final Date endTime, Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");

        Page<Opera> page = operaDao.findAll(new Specification<Opera>() {
            @Override
            public Predicate toPredicate(Root<Opera> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<Predicate>();
                if (StringUtils.isNotEmpty(name)) {
                    Predicate pre = cb.like(root.get("name").as(String.class), "%" + name + "%");
                    predicateList.add(pre);
                }
                if (StringUtils.isNotEmpty(description)) {
                    Predicate pre = cb.like(root.get("description").as(String.class), "%" + description + "%");
                    predicateList.add(pre);
                }
                if (null != startTime) {
                    Predicate pre = cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class), startTime);
                    predicateList.add(pre);
                }
                if (null != endTime) {
                    Predicate pre = cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), endTime);
                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageRequest);

        return page;
    }

    @Override
    public void addOperaInfo(HttpServletRequest request, String description) {
        Integer loginId = (Integer) request.getSession().getAttribute(Constant.CURRENT_USER_ID);

        SysUser sysUser = sysUserDao.findOne(loginId);

        Opera opera = new Opera();
        opera.setName(sysUser.getAccount());
        opera.setRoleName(sysUser.getRole().getName());
        opera.setDescription(description);
        opera.setCreateTime(new Date());

        operaDao.save(opera);
    }
}